<%@ page import="com.tlb.ejb.*,com.tlb.dto.*,com.tlb.web.*,java.util.*,java.net.URLEncoder" %>

<%

String u = request.getParameter("userName");
String p1 = request.getParameter("password1");
String p2 = request.getParameter("password2");
String m = request.getParameter("moniker");
String t = request.getParameter("tag");
String ilr = request.getParameter("inlineReply");
String offensive = request.getParameter("offensive");
String spam = request.getParameter("spam");

String threadId = request.getParameter("threadId");
String postId = request.getParameter("postId");
String treeId = request.getParameter("treeId");
String x = request.getParameter("x");
String action = request.getParameter("action");








int salt = (int)Math.round(Math.random()*10) + 10;

Moniker mon = new Moniker();
mon.setName(m);
mon.setTagLine(t);
mon.setPostCount(0);

Poster p = new Poster();
p.setUserName(u);
p.setPassword(p1);
p.setActive(1);
p.setInlineReply(ilr==null || ilr.startsWith("0")?0:1);

p.setNsfw(offensive != null && offensive.startsWith("1"));
p.setSpam(spam != null && spam.startsWith("1"));

p.setPrimaryMoniker(mon);
p.setSalt(salt);
mon.setPoster(p);

Poster existing = new PosterDAO().getPoster(u);
if(existing != null) {
    System.out.println("Attempt to register existing user: " + u);
%>
<div class="errorText">Attempt to register existing user: <%=u%></div>
<br>
<a href="javascript: history.go(-1)">Go Back</a>
<%
    return;
}


Moniker existingM = new MonikerDAO().getMoniker(m);
if(existingM != null) {
    System.out.println("Attempt to register existing moniker: " + m);
%>
<div class="errorText">Attempt to register existing moniker: <%=m%></div>
<br>
<a href="javascript: history.go(-1)">Go Back</a>
<%
    return;
}




new PosterDAO().savePoster(p, true);
p = new PosterDAO().getPoster(p.getPosterId());


System.out.println("registered new user: " + p.getUserName());
String uuid = new AuthManager().logUserIn(p.getPosterId());
Cookie c = new Cookie(AuthManager.AUTH_COOKIE_NAME, uuid);
c.setMaxAge(60*60*24*365);
response.addCookie(c);








if(action != null && action.equals("reply") && postId != null && threadId != null)
    //request.getRequestDispatcher("/replyStart.jsp?threadId=" + threadId + "&postId=" + postId + "&treeId=" + treeId + "&x=" + x).forward(request, response);
	request.getRequestDispatcher("jump.jsp?jumpToPage=" + URLEncoder.encode("replyStart.jsp?threadId=" + threadId + "&postId=" + postId + "&treeId=" + treeId + "&x=" + x, "UTF-8")).forward(request, response);
//else if(action != null && action.equals("edit") && postId != null && threadId != null)
//    request.getRequestDispatcher("/editStart.jsp?threadId=" + threadId + "&postId=" + postId).forward(request, response);
else if(action != null && action.equals("new"))
    //request.getRequestDispatcher("/newThread.jsp").forward(request, response);
	request.getRequestDispatcher("jump.jsp?jumpToPage=" + URLEncoder.encode("newThread.jsp","UTF-8")).forward(request, response);
else
	request.getRequestDispatcher("jump.jsp?jumpToPage=" + URLEncoder.encode("index.jsp","UTF-8")).forward(request, response);
    



%>
